package com.bcu.cn.onlineexamsystem.questionmanage.service.impl;

import com.bcu.cn.onlineexamsystem.questionmanage.entity.QuestionManage;
import com.bcu.cn.onlineexamsystem.questionmanage.mapper.QuestionManageMapper;
import com.bcu.cn.onlineexamsystem.questionmanage.service.QuestionManageService;
import com.bcu.cn.onlineexamsystem.exammanage.entity.Course;
import com.bcu.cn.onlineexamsystem.exammanage.mapper.CourseMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class QuestionManageServiceImpl implements QuestionManageService {
    
    private static final Logger logger = LoggerFactory.getLogger(QuestionManageServiceImpl.class);
    
    @Autowired
    private QuestionManageMapper questionManageMapper;
    
    @Autowired
    private CourseMapper courseMapper;
    
    @Override
    public List<QuestionManage> getQuestionsByTeacherId(String teacherId) {
        return questionManageMapper.findByTeacherId(teacherId);
    }
    
    @Override
    public QuestionManage getQuestionById(Long questionId) {
        return questionManageMapper.findById(questionId);
    }
    
    @Override
    public boolean addQuestion(QuestionManage question) {
        return questionManageMapper.insert(question) > 0;
    }
    
    @Override
    public boolean updateQuestion(QuestionManage question) {
        return questionManageMapper.update(question) > 0;
    }
    
    @Override
    public boolean deleteQuestion(Long questionId, String teacherId) {
        return questionManageMapper.delete(questionId, teacherId) > 0;
    }
    
    @Override
    public List<QuestionManage> getQuestionsByCourseId(String courseId, String teacherId) {
        return questionManageMapper.findByCourseId(courseId, teacherId);
    }
    
    @Override
    public List<Course> getTeacherCourses(String teacherId) {
        return courseMapper.findByTeacherId(teacherId);
    }
    
    @Override
    public Map<Course, List<QuestionManage>> getQuestionsGroupByCourse(String teacherId) {
        // 获取教师的所有课程
        List<Course> courses = getTeacherCourses(teacherId);
        logger.info("获取到的教师课程列表: {}", courses);
        
        // 获取教师的所有试题
        List<QuestionManage> questions = getQuestionsByTeacherId(teacherId);
        logger.info("获取到的教师试题列表: {}", questions);
        
        // 按课程ID分组试题
        Map<String, List<QuestionManage>> questionsByCourseId = questions.stream()
                .collect(Collectors.groupingBy(QuestionManage::getCourseId));
        logger.info("按课程ID分组的试题: {}", questionsByCourseId);
        
        // 构建课程-试题列表的映射
        Map<Course, List<QuestionManage>> result = new LinkedHashMap<>();
        for (Course course : courses) {
            // 确保设置teacherId
            course.setTeacherId(teacherId);
            // 获取该课程下的试题列表，如果为空则返回空列表
            List<QuestionManage> courseQuestions = questionsByCourseId.getOrDefault(course.getCourseId(), new ArrayList<>());
            logger.info("课程[{}]的试题列表: {}", course.getCourseName(), courseQuestions);
            result.put(course, courseQuestions);
        }
        
        logger.info("最终构建的课程-试题映射: {}", result);
        return result;
    }
} 